import nltk;

def discoveryMonthlyUptime(text):
    
    search = "<table </table>"
    stopWord = "Monthly Uptime Availability"
    dSearch = search.split()
    isTrue = True
    i = 0
    while(isTrue):
        start = text.find(dSearch[0],i) 
        finish = text.find(dSearch[1],i) 
        if start >=0:
            for i in stopWord.split():
                if text[start:finish].find(i) >=0:       
                    isTrue = False
        else:
            return False
        i = finish + 1
    print text[start:finish+8]   
    dicc = searchInTable(text[start:finish+8])
    
def searchInTable(text):
    search = "<td </td>"
    dSearch = search.split()
    dicc = {}
    for i in range(2):
        start = text.find(dSearch[0])
        count = start +1
        while(text[count]!='>'):
            text = text.replace(text[count],"",1)
        text = text.replace(text[count]," ",1)
        finish = text.find(dSearch[1])
        collumn = text[(count+1):finish]
        text = text[finish+5:]
        dicc.update({collumn:[]})
    start = text.find(dSearch[0])

    while(start>=0):
        count = start +1
        while(text[count]!='>'):
            text = text.replace(text[count],"",1)
        text = text.replace(text[count]," ",1)
        finish = text.find(dSearch[1])
        value = text[(count+1):finish]
        print value
        text = text[finish+5:]
        dicc[dicc.keys()[0]].append(value)
        
        start = text.find(dSearch[0])
        count = start +1
        while(text[count]!='>'):
            text = text.replace(text[count],"",1)
        text = text.replace(text[count]," ",1)
        finish = text.find(dSearch[1])
        value = text[(count+1):finish]
        text = text[finish+5:]
        dicc[dicc.keys()[1]].append(value)
        
        start = text.find(dSearch[0])
    return dicc

def normalized_words(article_text):
        oneline = article_text.replace('\n', ' ')
        cleaned = nltk.clean_html(oneline.strip())
        return cleaned
    
        
def countWords(text,word):
    print "WORD: " + word
    
    number = text.find(word) 
    count = 0 
    while(number>=0):
        text = text.replace(text[number:number+3],"",1)
        number = text.find(word)
        count = count +1
    return count 
if __name__ == '__main__':
    text = """
    <!-- /header -->
    </div> 
    <div id='bd'>
  <!-- layout product_detail -->
  <div id='yui-main'>
    <div class='yui-b'>
      <h1 class='title'>Amazon EC2 Service Level Agreement</h1>Vc eh feio
<h2>Effective Date: June 1, 2013</h2>
<p>This Amazon EC2 Service Level Agreement ('SLA') is a policy governing the use of Amazon Elastic Compute Cloud 
('Amazon EC2') and Amazon Elastic Block Store under the terms of the Amazon Web Services Customer
 Agreement between Amazon Web Services, Inc.  and users of AWS'
 services . This SLA applies separately to each account using Amazon EC2 or Amazon EBS. Unless otherwise 
 provided herein, this SLA is subject to the terms of the AWS Agreement and capitalized terms will have the 
 meaning specified in the AWS Agreement. We reserve the right to change the terms of this SLA in accordance 
 with the AWS Agreement.</p><h3>
"""
    text2 = """
    <table border="0"><tbody><tr><td>Monthly Availability % for a Given Region</td>
<td>Credit to Bill for HP Cloud Block Storage for a Given Region (not Total Bill)</td>
</tr><tr><td>100% to 99.95%</td>
<td>N/A</td>
</tr><tr><td>&lt;99.95% to 99.9%</td>
<td>5%</td>
</tr><tr><td>&lt;99.9% to 99.5%</td>
<td>10%</td>
</tr><tr><td>&lt;99.5% to 99.0%</td>
<td>20%</td>
</tr><tr><td>&lt;99.0%</td>
<td>30%</td>
</tr></tbody></table><h3><strong>Definitions</strong></h3>
"""
    print text2
    discoveryMonthlyUptime(text2)
    
    
    

 
    
    
    
    